﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace Web
{
    public partial class admin_newscategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            BindCategoryList();
            BindNotice();
        }
        private void BindNotice()
        {
            noticefailed.Visible = false;
            noticesuccess.Visible = false;
        }
        private void BindCategoryList()
        {
            List<NewsCategoryInfo> newscategorylist = new List<NewsCategoryInfo>();
            newscategorylist = NewsCategoryInfoLogic.GetAdminNewsCategoryInfo();//GetAllNewsCategoryInfo();
            foreach(var item in newscategorylist){
                item.NewsCount = NewsInfoLogic.GetCategoryNewsInfoCount(item.CategoryID);
            }
            CategoryListRpt.DataSource = newscategorylist;
            CategoryListRpt.DataBind();
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (Request.Form["ckb"]== null )
            {
                ChangeStatuc(false);
                return;
            }
            string[] tmp = Request.Form["ckb"].Split(',');
            int[] cid = new int[tmp.Count()];
            try
            {
                for (int i = 0; i < tmp.Count(); i++)
                {
                    cid[i] = Int32.Parse(tmp[i]);
                }
            }catch {
                ChangeStatuc(false);
                return;
            }
            int isSuccess = 0;
            foreach (var item in cid)
            {
                if (NewsInfoLogic.GetCategoryNewsInfoCount(item) > 0)
                {
                    //部分分类里面有新闻，需要先删除新闻
                    isSuccess = -1;
                    continue;
                }
                if ( NewsCategoryInfoLogic.DeleteNewsCategoryInfo(item) != 1){
                    //新闻分类删除失败
                    isSuccess = -2;
                    continue;
                }
            }
            if (isSuccess == 0)
            {
                //所有的删除操作已经成功
                ChangeStatuc(true);
            }else if(isSuccess == -1){
                noticefailedtext.InnerText = "新闻分类中仍有新闻，请先删除分类中的新闻！";
                ChangeStatuc(false);
            }
            else if (isSuccess == -2)
            {
                ChangeStatuc(false);
            }

            BindCategoryList();
        }

        /// <summary>
        /// 分类删除成功/失败之后的处理结果
        /// </summary>
        /// <param name="flag"></param>
        private void ChangeStatuc(bool flag)
        {
            noticefailed.Visible = flag?false:true;
            noticesuccess.Visible = flag?true:false;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();", true);
        }

        protected void ibtnDelete_OnClick(object sender, EventArgs e)
        {
            ImageButton ib = new ImageButton();
            int CategoryID = 0;
            try
            {
                ib = (ImageButton)sender;
                CategoryID =Int32.Parse( ib.ToolTip.ToString());
            }
            catch {
                return;
            }
            int isSuccess = -1;
            isSuccess = NewsCategoryInfoLogic.DeleteNewsCategoryInfo(CategoryID);
            if (isSuccess != 1)
            {
                ChangeStatuc(false);
            }
            else {
                ChangeStatuc(true);
            }
            //
            BindCategoryList();
        }
    }
}
